library(qualtRics)
library(dplyr)
library(estimatr)
library(tidyr)
library(ggplot2)
library(magrittr)
library(broom)
library(hrbrthemes)
library(modelsummary)
library(texreg)

theme_params <-  theme(panel.background = element_rect(fill = 'gray95'),
                       panel.grid.minor = element_blank(),
                       panel.grid.major = element_blank(),
                       legend.text = element_text(size = 15),
                       axis.text.x = element_text(size = 15),
                       legend.title=element_blank(),
                       axis.text.y = element_text(size = 15),
                       axis.title.y = element_text(size = 15),
                       axis.title.x = element_text(size = 15),
                       legend.position = "bottom",
                       plot.margin = unit(c(.25,.25,.25,.25), "cm"),
                       strip.text.x = element_text(size = 15))

study4 <- qualtRics::read_survey("data/study4.csv")

study4 %>%
 filter(moderation_failure == 0) %>%
 dplyr::select(birthyr, educ, income, ideology = ideology...26, issue,
        canned, generic_flag, contains("polsoph"),
        likert = post_outcome_1, certainty = post_certainty_19,
        mvc_check, pid3, pidlean, pidstrength,
        treatment, contains("pw2p"), contains("participation")) %>%
 mutate(age = 2023-birthyr,
           educ = educ,
           income = income,
           ideology,
           likert,
           certainty,
           treatment) -> study4_coded

study4_coded %<>%
 mutate(polsoph1 = as.numeric(polsoph1 == 2),
        polsoph2 = as.numeric(polsoph2 == 1),
        polsoph3 = as.numeric(grepl("2/3|thirds|67", polsoph3, ignore.case = T)),
        polsoph4 = as.numeric(grepl("court|judicial|scotus", polsoph4, ignore.case = T)),
        polsoph5 = as.numeric(grepl("vice|vp", polsoph5, ignore.case = T)))

study4_coded$pk_scale <- study4_coded %>% dplyr::select(contains("polsoph")) %>% rowSums()/5

# Create a 7-point partisanship scale
study4_coded %<>%
 mutate(
  pid7 = case_when(
   # Strong Democrat
   pid3 == 1 & pidstrength == 2 ~ 1,
   # Weak Democrat
   pid3 == 1 & pidstrength == 4 ~ 2,
   # Lean Democrat
   pid3 == 3 & pidlean == 2 ~ 3,
   # Independent / No lean
   pid3 == 3 & pidlean == 3 ~ 4,
   # Lean Republican
   pid3 == 3 & pidlean == 1 ~ 5,
   # Weak Republican
   pid3 == 2 & pidstrength == 4 ~ 6,
   # Strong Republican
   pid3 == 2 & pidstrength == 2 ~ 7,
   TRUE ~ NA_real_
  )
 )

study4_coded %>%
 mutate(pw2p_scale = (abbreviated_pw2p_1+
                          abbreviated_pw2p_2+
                          abbreviated_pw2p_3+
                          abbreviated_pw2p_4+
                          abbreviated_pw2p_5+
                          (8-abbreviated_pw2p_6)+
                          abbreviated_pw2p_7+
                          abbreviated_pw2p_8)/8,
        pw2p_defend = ((8-pw2p_defend_abbrev_1) +
                        pw2p_defend_abbrev_2 +
          (8-pw2p_defend_abbrev_3)+
         pw2p_defend_abbrev_4+
         (8-pw2p_defend_abbrev_5))/5)-> study4_coded

study4_coded$participation_scale <- psych::alpha(study4_coded %>%
                                                 dplyr::select(contains("participation")))$scores

minmax <- function(x) ((x - min(x,na.rm=T))/(max(x,na.rm=T) - min(x,na.rm=T)))
